package cn.com.dhc.danlu.widget.base;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import cn.com.dhc.danlu.util.CommonUtils;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-09-13
 * Time: 16:11
 * Description:
 */
public class BasePopupWindow extends PopupWindow {
    private static final float SHOW_ALPHA = 0.68f;
    private boolean isNeedAnim = true;

    public BasePopupWindow(Context context) {
        super(context);
        setPopupWindowDefaultAttr();
    }

    private void setPopupWindowDefaultAttr() {
        isNeedAnim = true;
        setBackgroundDrawable(new BitmapDrawable());
        //必须显示宽和高
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public void setNeedAnim(boolean needAnim) {
        isNeedAnim = needAnim;
    }

    @Override
    public void setContentView(View contentView) {
        if (null != contentView) {
            contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            super.setContentView(contentView);
            addKeyListener(contentView);
        }
    }

    /**
     * 默认设置该方法是不会生效的 必须在外部
     * setBackgroudDrawable
     */
    @Override
    public void setOutsideTouchable(boolean touchable) {
        super.setOutsideTouchable(touchable);
        if (touchable) {
            super.setBackgroundDrawable(new BitmapDrawable());
        } else {
            super.setBackgroundDrawable(null);
        }
    }

    //  属性动画 背景变暗
    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        if (isNeedAnim) {
            createAnim(true).start();
        }
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        if (isNeedAnim) {
            createAnim(true).start();
        }

    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);
        if (isNeedAnim) {
            createAnim(true).start();
        }
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        if (isNeedAnim) {
            createAnim(true).start();
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (isNeedAnim) {
            createAnim(false).start();
        }
    }

    /**
     * 为窗体添加outside点击事件
     */
    private void addKeyListener(View contentView) {
        if (contentView != null) {
            contentView.setFocusable(true);
            contentView.setFocusableInTouchMode(true);
            contentView.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int keyCode, KeyEvent event) {
                    switch (keyCode) {
                        case KeyEvent.KEYCODE_BACK:
                            dismiss();
                            return true;
                        default:
                            break;
                    }
                    return false;
                }
            });
        }
    }

    private ValueAnimator createAnim(boolean isShowAnimation) {
        ValueAnimator va;
        if (isShowAnimation) {
            // 展示动画
            va = ValueAnimator.ofFloat(1.0f, SHOW_ALPHA);
        } else {
            //消失动画
            va = ValueAnimator.ofFloat(SHOW_ALPHA, 1.0f);
        }
        va.setDuration(360);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float alpha = (float) animation.getAnimatedValue();
                setWindowBackgroundAlpha(alpha);
            }
        });
        return va;
    }

    private void setWindowBackgroundAlpha(float alpha) {
        Window window = CommonUtils.getActivity(getContentView()).getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.alpha = alpha;
        window.setAttributes(layoutParams);
    }
}
